package com.xuecheng.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.learning.feignclient.MediaFileClient;
import com.xuecheng.learning.feignclient.TeachplanClient;
import com.xuecheng.learning.mapper.LearnRecordMapper;
import com.xuecheng.learning.model.dto.CourseTablesDto;
import com.xuecheng.learning.model.dto.MyCourseTableParams;
import com.xuecheng.learning.model.po.CourseTables;
import com.xuecheng.learning.model.po.LearnRecord;
import com.xuecheng.learning.service.CourseTablesService;
import com.xuecheng.learning.service.LearningService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class LearningServiceImpl extends ServiceImpl<LearnRecordMapper, LearnRecord>
    implements LearningService {
    @Autowired
    CourseTablesService courseTablesService;

    @Autowired
    TeachplanClient teachplanClient;

    @Autowired
    MediaFileClient mediaFileClient;

    /**
     * 获取对应课程计划的视频路径
     * @param userId
     * @param courseId
     * @param teachplanId
     * @param mediaId
     * @return
     */
    @Override
    public RestResponse<String> getVideo(Long userId, Long courseId, Long teachplanId, Long mediaId) {
        //先判断当前学生是否有学习当前课程的资格
        CourseTablesDto learnStatus = courseTablesService.getLearnStatus(userId, courseId);
        if(!"702001".equals(learnStatus.getLearnStatus())){
            //如果没有资格，再判断当前小节是否支持试学
            Teachplan teachplan = teachplanClient.getById(teachplanId);
            if(!"1".equals(teachplan.getIsPreview())){
                //本小节不支持试学
                return RestResponse.validfail("当前课程无学习资格");
            }
        }
        //有学习资格，下面就获取该小节对应的视频路径
        return mediaFileClient.preview(mediaId);
    }

    /**
     * 根据条件查询我的课程
     * @param params
     * @return
     */
    @Override
    public PageResult<CourseTables> getMyCourseTable(MyCourseTableParams params) {
        Page<CourseTables> pageInfo = new Page<>(params.getPage(), params.getSize());
        String courseType = params.getCourseType();
        String expiresType = params.getExpiresType();
//        String sortType = params.getSortType();
        boolean blank = StringUtils.isNotBlank(courseType);
        boolean blank1 = StringUtils.isNotBlank(expiresType);
        courseTablesService.page(pageInfo,new LambdaQueryWrapper<CourseTables>()
                .eq(CourseTables::getUserId,params.getUserId())
                .eq(blank,CourseTables::getCourseType,courseType)
                .gt(blank1,CourseTables::getValidtimeEnd, LocalDateTime.now()));
        return new PageResult<>(pageInfo.getRecords(),pageInfo.getCurrent(),pageInfo.getSize(),
                pageInfo.getTotal());
    }
}
